package kr.co.doriclassic.summary_java.serviceUI;

import javax.swing.JPanel;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.co.doriclassic.summary_java.dao.DepartmentDao;
import kr.co.doriclassic.summary_java.dao.EmployeeDao;
import kr.co.doriclassic.summary_java.dao.InterfaceDao;
import kr.co.doriclassic.summary_java.dto.CodeTitle;
import kr.co.doriclassic.summary_java.dto.Department;
import kr.co.doriclassic.summary_java.dto.Employee;

public class PanelTable<T> extends JPanel implements ActionListener {
	private JTable table;
	private JLabel lblNewLabel;
	private InterfaceDao<T> dao;
	private JScrollPane scrollPane;
	private JPanel panel;
	private JTextField searchTf;
	private JButton btnSearch;
	private Object[][] arrs;
	private ArrayList<T> list;
	private T temp;
	private String[] colNames;

	public InterfaceDao<T> getDao() {
		return dao;
	}

	public void setDao(InterfaceDao<T> dao) {
		this.dao = dao;
		inputTable();
	}

	public String getLblNewLabel() {
		return lblNewLabel.getText();
	}

	public void setLblNewLabel(String s) {
		lblNewLabel.setText(s);
	}

	public PanelTable() {
		initialize();
	}

	public ArrayList<T> getList() {
		return list;
	}

	public void setList(ArrayList<T> list) {
		this.list = list;
	}

	private void initialize() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		lblNewLabel = new JLabel("New label");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		add(lblNewLabel);

		panel = new JPanel();
		add(panel);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		searchTf = new JTextField();
		panel.add(searchTf);
		searchTf.setColumns(10);

		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel.add(btnSearch);

		scrollPane = new JScrollPane();
		add(scrollPane);

		table = new JTable();

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				T item = null;
				try {
					temp = dao.searchItem(item);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				removeAll();
				addTable();
				revalidate();
			}

			private void addTable() {
				if (temp instanceof Employee) {
					Employee emp = new Employee();
					int no = (int) table.getValueAt(table.getSelectedRow(), 0);
					String title = table.getValueAt(table.getSelectedRow(), 2)+ "";
					String manager = table.getValueAt(table.getSelectedRow(), 3)+ "";
					String dept = table.getValueAt(table.getSelectedRow(), 5)+ "";
					((Employee) temp).setEmpNo(no);
					add(new PanelEmployeetUI(emp, title, manager, dept));
				} else if (temp instanceof Department) {
					Department dept = new Department();
					int no = (int) table.getValueAt(table.getSelectedRow(), 0);
					dept.setDeptNo(no);
					add(new PanelDepartmentUI(dept));
				} else {

				}
			}
		});

		table.setAutoCreateRowSorter(true);// 탭정렬
		scrollPane.setViewportView(table);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSearch) {
			try {
				this.removeAll();
				list = dao.searchList(searchTf.getText().trim());
				setList(list);
				setDao(dao);
				revalidate();
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
			} catch (NullPointerException e1) {
				JOptionPane.showMessageDialog(this, "결과가 없습니다.");

			}
		}
	}

	private void inputTable() {
		try {
			list = dao.listItems();
			System.out.println(list.size());
			arrs = new Object[list.size()][6];
			for (int i = 0; i < list.size(); i++) {
				temp = list.get(i);
				if (temp instanceof Employee) {
					colNames = new String[] { "사원번호", "사원명", "직급", "매니저", "급여",
							"부서" };
					arrs[i][0] = ((Employee) temp).getEmpNo();
					arrs[i][1] = ((Employee) temp).getEmpName();
					arrs[i][2] = ((EmployeeDao) dao)
									.selectedTitleByTitleNo(((Employee) temp)
									.getTitle());
					arrs[i][3] = ((EmployeeDao) dao).selectedNameByEmpNo(((Employee) temp).getManager());
					arrs[i][4] = ((Employee) temp).getSalary();
					arrs[i][5] = ((EmployeeDao) dao)
									.selectedDeptNameByDeptNo(((Employee) temp)
									.getDeptno());
				}
				else if(temp instanceof Department){
					colNames = new String[]{"부서번호","부서명","위치"};
					arrs[i][0]=((Department) temp).getDeptNo();
					arrs[i][1]=((Department) temp).getDeptName(); 
					arrs[i][2]=((Department) temp).getFloor();
				} else {
					colNames=new String[] {"직급번호", "직급명"}; 
					arrs[i][0]=((CodeTitle) temp).getTitleNo();
					arrs[i][1]=((CodeTitle) temp).getTitleName();
				}
			}			
		} catch (SQLException e1) {
			e1.printStackTrace();
		} finally {
			TableModel model = new DefaultTableModel(arrs, colNames);
			table.setModel(model);
		}
		revalidate();
	}
}
